package com.geekcattle.controller.user;

import com.geekcattle.core.shiro.CustomerAuthenticationToken;
import com.geekcattle.service.user.UserService;
import com.geekcattle.util.DateUtil;
import com.geekcattle.util.MapUtil;
import com.geekcattle.util.PasswordUtil;
import com.geekcattle.util.UuidUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: geek-framework
 * @description:
 * @author: LQ
 * @create: 2019-05-05 20:56
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserService userService;

    /**
     * 添加普通用户
     *
     * @param map
     * @return
     */
    @Transactional
    @RequestMapping(value = "/addUser", method = {RequestMethod.GET, RequestMethod.POST})
    public Map addUser(@RequestParam Map map) {
        System.out.println("map:" + map);
        if (map == null) {
            return MapUtil.infoMap(0,"添加用户失败,传递参数为空");
        }
        if (map.get("username") == null || "".equals(map.get("username"))) {
            return MapUtil.infoMap(0,"添加用户失败,用户名不能为空");
        }
        String username = map.get("username") + "";
        List<Map> list = userService.getUser(map);
        if (list.size() > 0) {
            return MapUtil.infoMap(0,"添加用户失败,用户名已经存在");
        }
        String uid = UuidUtil.getUUID();
        map.put("uid", uid);
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
        map.put("salt", salt);
        String password = PasswordUtil.createAdminPwd(map.get("password") + "",username+salt);
        map.put("password", password);
        map.put("is_system", 0);
        map.put("created_at", DateUtil.getCurrentTime());
        map.put("updated_at", DateUtil.getCurrentTime());
        try {
            userService.addUser(map);
            //            添加角色关联表
            String roleId = "a570f6377cf34985b854d6ed04f50ca7";
            Map m = new HashMap();
            m.put("admin_id", uid);
            m.put("role_id", roleId);
            userService.addRole(m);
            return MapUtil.infoMap(1,"添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return MapUtil.infoMap(0,"添加用户失败");
        }
    }

    @RequestMapping(value = "/updatePassWord", method = {RequestMethod.POST, RequestMethod.GET})
    public Map updatePassWord(@RequestParam Map adminMap) {
        System.out.println("adminMap:" + adminMap);
        String username = adminMap.get("username") + "";
        CustomerAuthenticationToken token = new CustomerAuthenticationToken(username, adminMap.get("oldPassword") + "", false);
        Subject currentUser = SecurityUtils.getSubject();
        try {
            //在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查
            //每个Realm都能在必要时对提交的AuthenticationTokens作出反应
            //所以这一步在调用login(token)方法时,它会走到MyRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法
            currentUser.login(token);
        } catch (Exception e) {
//            e.printStackTrace();
            return MapUtil.infoMap(0, "原密码错误");
        }
        if (!(adminMap.get("newPassword") + "").equals(adminMap.get("surePassword") + "")) {
            return MapUtil.infoMap(0, "两次密码不一致");
        }
        //盐
        String salt = (new SecureRandomNumberGenerator().nextBytes().toHex());
        adminMap.put("salt", salt);
        //组合密码
        String password = PasswordUtil.createAdminPwd(adminMap.get("newPassword") + "", username + salt);
        adminMap.put("password", password);
        adminMap.put("updated_at", DateUtil.getCurrentTime());
        int row = userService.updateUser(adminMap);
        if (row > 0) {
            return MapUtil.infoMap(1, "密码修改成功");
        } else {
            return MapUtil.infoMap(0, "密码修改失败");
        }
    }
}
